''' IBAN Functions - IBANAlgorithm.vb
''' 
''' This program is free software; you can redistribute it and/or modify
''' it under the terms of the GNU General Public License as published by
''' the Free Software Foundation; either version 2 of the License, or
''' (at your option) any later version.
'''
''' This program is distributed in the hope that it will be useful,
''' but WITHOUT ANY WARRANTY; without even the implied warranty of
''' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
''' GNU General Public License for more details.
'''
''' You should have received a copy of the GNU General Public License
''' along with this program; if not, write to the Free Software
''' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

''' -----------------------------------------------------------------------------
''' Project	 : IBANFuncsCore
''' Class	 : Core.IBANAlgorithm
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' This class implements the IBAN check digits creation algorithm.
''' </summary>
''' <remarks>
''' The check digits are created based on the mod 97/10 algorithm.
''' The class provides shared (static) methods only.
''' </remarks>
''' <history>
''' 	[Nick]	2/8/2007	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class IBANAlgorithm

#Region "Public methods"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Creates an IBAN from the related elements.
    ''' </summary>
    ''' <param name="CountryCode">IBAN country code.</param>
    ''' <param name="BankCode">IBAN bank code.</param>
    ''' <param name="BranchCode">IBAN branch code.</param>
    ''' <param name="AccountNumber">IBAN account number.</param>
    ''' <returns>IBAN string complete with check digits.</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function CreateIBAN(ByVal CountryCode As String, _
                                      ByVal BankCode As String, _
                                      ByVal BranchCode As String, _
                                      ByVal AccountNumber As String) As String

        Dim s1 As String = BankCode + BranchCode + AccountNumber + CountryCode + "00", s2 As String = ""

        'Substitute letters
        For i As Integer = 0 To s1.Length - 1
            If IsNumeric(s1.Substring(i, 1)) = True Then
                s2 += s1.Substring(i, 1)
            Else
                s2 += Convert.ToString(Asc(s1.Substring(i, 1)) - 55).PadLeft(2, "0")
            End If
        Next

        'Return the IBAN
        Return CountryCode + MOD_97_10(s2) + BankCode + BranchCode + AccountNumber

    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Creates an IBAN from the related elements.
    ''' </summary>
    ''' <param name="Account"><see cref="IBANContents"/> with the account elements.</param>
    ''' <returns>IBAN string complete with check digits.</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function CreateIBAN(ByVal Account As IBANContents) As String

        Return CreateIBAN(Account.CountryCode, Account.BankCode, Account.BranchCode, Account.AccountNumber)

    End Function

#End Region

#Region "Private methods"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Calculates the IBAN check digits using the mod97 algorithm
    ''' </summary>
    ''' <param name="s">String to use for the calculation.</param>
    ''' <returns>Check digits string, 2 characters.</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Nick]	2/8/2007	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Shared Function MOD_97_10(ByVal s As String) As String

        Dim s1 As String, s2 As String
        Dim l1 As Integer, l2 As Integer, l3 As Integer

        s1 = s.Substring(0, 9)
        s2 = s.Substring(s1.Length)
        l1 = Convert.ToInt32(s1)
        l2 = l1 Mod 97

        While s2 <> ""
            If Len(s2) > 7 Then
                s1 = Convert.ToString(l2).PadLeft(2, "0") + s2.Substring(0, 7)
                s2 = s2.Substring(7)
            Else
                s1 = Convert.ToString(l2).PadLeft(2, "0") + s2
                s2 = ""
            End If
            l1 = Convert.ToInt32(s1)
            l2 = l1 Mod 97
        End While

        Return Convert.ToString(98 - l2).PadLeft(2, "0")

    End Function

#End Region

End Class
